package com.w1.security.handler.authHandler;

import com.alibaba.fastjson.JSON;
import com.w1.security.domain.ResponseResult;
import com.w1.security.utiles.WebUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证过程失败处理器
 * @author mingyifan
 * @date 2022-02-24 16:18
 */
@Component
public class AuthFailureHandler implements AuthenticationFailureHandler {
    @Value("${securityProperties.auto401}")
    private String auto401;
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        System.out.println("认证过程失败");
        ResponseResult responseResult = new ResponseResult(HttpStatus.FORBIDDEN.value(),"认证过程失败");
        WebUtils.renderString(response, JSON.toJSONString(responseResult));

    }
}
